package edu.njfu.networkdemo

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import edu.njfu.networkdemo.utils.HttpUtil
import kotlinx.android.synthetic.main.activity_sixth.*
import okhttp3.Call
import okhttp3.Response
import java.io.IOException
import java.io.InputStream
import kotlin.concurrent.thread

class SixthActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sixth)
        val downloadImage=1
        val handler=object : Handler(){
            override fun handleMessage(msg: Message) {
                when(msg.what){
                    downloadImage-> {
                        Log.e("test","test................")
                        imageView.setImageBitmap(msg.obj as Bitmap)
                        Log.e("end","end..............")
                    }
                }
            }
        }
        HttpUtil.sendOkHttpRequest("http://10.17.92.18:8080/images/students/boy.jpeg",object :okhttp3.Callback{
            override fun onFailure(call: Call, e: IOException) {

            }

            override fun onResponse(call: Call, response: Response) {
                    val image = BitmapFactory.decodeStream(response.body?.byteStream())
                    val msg = Message()
                    msg.what = downloadImage
                    msg.obj = image
                thread {
                    handler.handleMessage(msg);
                }

                }
        })
    }
}